/*=================================================================================
create_tts_monitored_columns_table.sql - Created by Dave Herrington, DARC Corporation


Copyright (c) 2009, Dave Herrington, DARC Corporation and Proofspace, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.


Comments:

This is meta data table that contains a row for each application column to be
monitored for changes and to have Trusted Timestamps generated for these changes.

Sample scripts for creating meta data in this table have been provided with
this distribution.

Run this script connected to the TTS_ADMIN schema.  The password to TTS_ADMIN
should be kept Top Secret.


Usage:

sqlplus TTS_ADMIN/<TTS_ADMIN Password> @create_tts_monitored_columns_table.sql


History:

08/06/09 D.Herrington - Created.

=================================================================================*/

DROP SEQUENCE tts_monitored_columns_seq;

CREATE SEQUENCE tts_monitored_columns_seq START WITH 1;

CREATE TABLE tts_monitored_columns (
	column_id		NUMBER				NOT NULL,
	column_sequence		NUMBER				NOT NULL,
	column_name		VARCHAR2(30)			NOT NULL,
	column_type		CHAR(1)		DEFAULT 'A'  	NOT NULL
		CHECK ( column_type IN ('P' , 'F' , 'A' , 'p' , 'f' , 'a')), 
	table_id		NUMBER				NOT NULL,
	description		VARCHAR2(1000),
	creation_date		DATE		DEFAULT SYSDATE	NOT NULL,
   	created_by		VARCHAR2(30)	DEFAULT USER	NOT NULL,
	last_update_date	DATE		DEFAULT SYSDATE	NOT NULL,
   	last_updated_by		VARCHAR2(30)	DEFAULT USER	NOT NULL)
LOGGING;

CREATE OR REPLACE TRIGGER tts_monitored_columns_ins
BEFORE INSERT
ON tts_monitored_columns
FOR EACH ROW
BEGIN
  SELECT tts_monitored_columns_seq.nextval
  INTO :new.column_id
  FROM dual;
END;
/

ALTER TABLE tts_monitored_columns
    ADD CONSTRAINT tts_monitored_columns_pk PRIMARY KEY ( column_id ) ;

ALTER TABLE tts_monitored_columns 
    ADD CONSTRAINT tts_monitored_columns_tabid_fk FOREIGN KEY 
    ( 
     table_id
    ) 
    REFERENCES tts_monitored_tables 
    ( 
     table_id
    ) 
    NOT DEFERRABLE 
;